﻿using SK3000RunningTestTool.Properties;
using SK3000RunningTestTool.SKServerDelegate;
using SK3000RunningTestTool.SKServerEnum;
using SK3000RunningTestTool.SKServerSource;
using SK3000RunningTestTool.SKService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SK3000RunningTestTool
{
    public partial class Frm_Main : DevComponents.DotNetBar.Metro.MetroForm
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            Init();
        }

        public void Init()
        {
            SKServerSource.ServerSource.Init();
            Init_CreateTree();
        }

        public bool Init_CreateTree()
        {
            bool bolResult = false;
            tvServer.Nodes.Clear();
            foreach (SKServer server in SKServerSource.ServerSource.SKServerList)
            {
                TreeNode tn = new TreeNode();
                server.ServerStatusChangedEvent += Server_ServerStatusChangedEvent;
                tn.Name = "tn_S_" + server.ServerID;
                tn.Text = server.ServerName + "[" + ServerSource.GetServerStatusString(server.ServerStatus) + "]";
                foreach (ISKService service in server.ServiceList)
                {
                    tn.Nodes.Add(GetTreeNodeByISKService(service));
                }
                tn.ImageKey = tn.SelectedImageKey = "img_serverstaus_default.png";
                server.Tag = tn;//方便状态改变时修改图标等操作，小项目可以这样使用，实际大项目/或者为公用静态的变量不推荐使用
                tn.Tag = server;
                tvServer.Nodes.Add(tn);
                server.ServerStatusRefreshAsync();
            }
            return bolResult;
        }

        private void Server_ServerStatusChangedEvent(object sender, object ServerStatusChangedValue)
        {
            SKServer server = (SKServer)sender;
            TreeNode tn = (TreeNode)server.Tag;
            this.BeginInvoke(new EventHandler(delegate
            {
                tn.ImageKey = tn.SelectedImageKey = server.ServerStatus == Enum_ServerStatus.InService ? "img_serverstaus_normal.png" : "img_serverstaus_default.png";
                tn.Text = server.ServerName + "[" + ServerSource.GetServerStatusString(server.ServerStatus) + "]";
                tn.ForeColor = server.ServerStatus == Enum_ServerStatus.InService ? Color.Green : Color.Black;
            }));
        }

        public TreeNode GetTreeNodeByISKService(ISKService iservice)
        {
            TreeNode tn = new TreeNode();
            tn.Name = "tn_Service_" + iservice.ID;
            tn.Text = iservice.ServiceName;
            tn.ImageKey = tn.SelectedImageKey = "img_servicestaus_default.png";
            iservice.Tag = tn;//方便状态改变时修改图标等操作，小项目可以这样使用，实际大项目/或者为公用静态的变量不推荐使用
            tn.Tag = iservice;
            iservice.ServiceTestCallbackEvent += Result_ServiceTestCallbackEvent;
            return tn;
        }



        private void Result_ServiceTestCallbackEvent(object sender, ServiceTestCallbackValue ServiceTestCallbackValue)
        {
            ISKService test = (ISKService)sender;
            TreeNode tn = (TreeNode)test.Tag;
            this.BeginInvoke(new EventHandler(delegate
            {
                //string strPrompt = test.Server.ServerName + " [" + test.ServiceName + "]";
                string strPrompt = "";
                switch (ServiceTestCallbackValue.EventValue)
                {
                    case Enum_ServiceTestEvent.ServiceTestStart:
                        strPrompt += "[服务测试开始]";
                        tn.ImageKey = tn.SelectedImageKey = "img_servicestaus_default.png";
                        tn.ForeColor = Color.Black;
                        break;
                    case Enum_ServiceTestEvent.ServiceTestEnd:
                        strPrompt += "[服务测试结束]";
                        break;
                    case Enum_ServiceTestEvent.ConnectServiceing:
                        strPrompt += "[连接服务中]";
                        break;
                    case Enum_ServiceTestEvent.ConnectSuccess:
                        strPrompt += "[服务连接成功]";
                        break;
                    case Enum_ServiceTestEvent.ConnectException:
                        strPrompt += "[服务连接失败]";
                        break;
                    case Enum_ServiceTestEvent.ConnectServiced:
                        strPrompt += "[服务连接完成]";
                        break;
                    case Enum_ServiceTestEvent.SendTestDataing:
                        strPrompt += "[发送测试数据]" + ServiceTestCallbackValue.EventMessage;
                        break;

                    case Enum_ServiceTestEvent.SendTestDataSuccess:
                        strPrompt += "[收到返回数据]" + ServiceTestCallbackValue.EventMessage;
                        break;

                    case Enum_ServiceTestEvent.SendTestDataException:
                        strPrompt += "[测试数据失败]";
                        break;
                    case Enum_ServiceTestEvent.ConnectCloseing:
                        strPrompt += "[关闭服务连接]";
                        break;
                    case Enum_ServiceTestEvent.ConnectClosed:
                        strPrompt += "[服务连接完成]";
                        break;
                }
                tn.Text = test.ServiceName + strPrompt;
                DisplayInfo(test.Server.ServerName + " [" + test.ServiceName + "]" + strPrompt);
            }));
            Console.WriteLine(DateTime.Now.ToString() + "执行测试结束，结果:" + ServiceTestCallbackValue.EventValue.ToString());
        }

        public bool DisplayInfo(string s)
        {
            bool bolResult = false;
            lstMsg.Items.Add(DateTime.Now.ToString("HH:mm:ss") + "" + s);
            return bolResult;
        }


        public void ServiceListTest(SKServer server )
        {
            foreach (ISKService service in server.ServiceList)
            {
                service.ServiceTestAsync(new ServiceTestCompleteDelegate(ServiceTestComplete));
            }
        }

        public void ServiceTestComplete(object sender, ServiceTestCompleteValue valuess)
        {
            ISKService iservice = (ISKService)sender;
            this.BeginInvoke(new EventHandler(delegate
            {
                TreeNode tn = (TreeNode)iservice.Tag;
                string strPormpt = "";
                switch (valuess.reuslt)
                {
                    case Enum_ServiceTestResult.Normal:
                        strPormpt = "[服务正常]";
                        tn.ImageKey = tn.SelectedImageKey = "img_servicestaus_normal.png";
                        tn.ForeColor = Color.Green;
                        break;
                    case Enum_ServiceTestResult.Fail:
                        strPormpt = "[服务异常]" + Convert.ToString(valuess.Para1);
                        tn.ImageKey = tn.SelectedImageKey = "img_servicestaus_fail.png";
                        tn.ForeColor = Color.Red;
                        break;
                    case Enum_ServiceTestResult.TestException:
                        strPormpt = "[服务异常]" + Convert.ToString(valuess.Para1);
                        tn.ImageKey = tn.SelectedImageKey = "img_servicestaus_fail.png";
                        tn.ForeColor = Color.Red;
                        break;
                    default:
                        break;
                }
                tn.Text = iservice.ServiceName + strPormpt;
            }));
        }


        private void btnTestAll_Click(object sender, EventArgs e)
        {
            foreach (SKServer server in SKServerSource.ServerSource.SKServerList)
            {
                if (server.ServerStatus == Enum_ServerStatus.InService || server.ServerStatus == Enum_ServerStatus.Unknown)
                {
                    //正常，未知状态进行检测
                    ServiceListTest(server);
                }
            }
        }

        private void btnMsgClear_Click(object sender, EventArgs e)
        {
            lstMsg.Items.Clear();
        }

        private void tvServer_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tvServer.SelectedNode = tvServer.GetNodeAt(e.X, e.Y);//选中该节点
            }
        }

        private void tsmiTest_Click(object sender, EventArgs e)
        {
            if (typeof(SKServer) == tvServer.SelectedNode.Tag.GetType())
            {
                //Server
                SKServer server = (SKServer)tvServer.SelectedNode.Tag;
                if (server.ServerStatus == Enum_ServerStatus.InService || server.ServerStatus == Enum_ServerStatus.Unknown)
                {
                    //正常，未知状态进行检测
                    ServiceListTest(server);
                }
                else
                {
                    MessageBox.Show("请选择正在运行中的服务器");
                }
            }
            else if (typeof(ISKService).IsAssignableFrom(tvServer.SelectedNode.Tag.GetType()))
            {
                //Service
                ISKService service = (ISKService)tvServer.SelectedNode.Tag;
                SKServer server = service.Server;
                if (server.ServerStatus == Enum_ServerStatus.InService || server.ServerStatus == Enum_ServerStatus.Unknown)
                {
                    //正常，未知状态进行检测
                    service.ServiceTestAsync(new ServiceTestCompleteDelegate(ServiceTestComplete));
                }
                else
                {
                    MessageBox.Show("请选择正在运行中的服务器");
                }
            }
        }
    }
}
